﻿app.factory("ActiveGraphs", function () {
    return [];
})

app.controller("ngHomeController", function ($scope, $http, ActiveGraphs, $window, LoginService, $location, $route, $routeParams) {
    $scope.graphs = ActiveGraphs;
    $scope.graphType = "df";
    $scope.layoutType = "grid";
    var setId = $routeParams.id;

    var MrGraphie = new CanvasGraph();
    var GetGraph = new GraphTemplates();
    var bundler = new MrGraphieBundler();


    $scope.currentGraph;

    $scope.canvSize = {
        xMin: -10,
        xMax: 10,
        yMin: -10,
        yMax: 10
    };

    $scope.graphTemplate;
    $scope.graphParams;

    $scope.flipGrid = function () {
        if ($scope.layoutType == "grid") {
            $scope.layoutType = "tick";
        }
        else {
            $scope.layoutType = "grid";
        }
        delete $scope.wasRendered;
        canvasInfo.backgroundStyle = $scope.layoutType;
        $scope.renderGraph();
    }

    $scope.makeActive = function (graph) {
        $scope.graphType = "df";
        $scope.graphTemplate = graph;
        $scope.currentGraph = graph;
        $scope.graphParams = graph.graphParams;
        $scope.renderGraph();
    };

    $scope.addGraph = function () {
        if (!$scope.currentGraph) {
            return;
        }

        $scope.graphs.push($scope.currentGraph);
        $scope.makeActive($scope.currentGraph);
        //$scope.currentGraph = null;
        //$scope.setCurrentGraph(true);
    }

    var canvasInfo = {
        xMin: -10,
        xMax: 10,
        yMin: -10,
        yMax: 10,
        //canvasColor: "black",
        //guideColor: "gray",
        //axisColor: "white",
        backgroundStyle: $scope.layoutType,
        guideWidth: 0.5,
        axisWidth: 0.5
    };

    $scope.setCurrentGraph = function (deferRender) {
        if ($scope.graphType == "df") {
            return;
        }
        $scope.currentGraph = null;
        $scope.graphTemplate = GetGraph.getTemplate($scope.graphType);
        $scope.graphParams = $scope.graphTemplate.graphParams;
        if (!deferRender) {
            $scope.currentGraph = $scope.graphTemplate;
            $scope.renderGraph();

        }
    };

    $scope.removeGraph = function () {
        for (var i in $scope.graphs) {
            if ($scope.graphs[i] === $scope.currentGraph) {
                $scope.graphs.splice(i, 1);
                break;
            }
        }
        $scope.currentGraph = null;
        $scope.graphTemplate = null;
        $scope.graphParams = null;
        $scope.renderGraph();
    };

    $scope.adjustParams = function () {
        if (!$scope.currentGraph) {
            $scope.currentGraph = $scope.graphTemplate;
        }
        $scope.renderGraph();
    }

    $scope.renderGraph = function () {
        delete canvasInfo.wasRendered;

        canvasInfo.xMin = $scope.canvSize.xMin;
        canvasInfo.xMax = $scope.canvSize.xMax;
        canvasInfo.yMin = $scope.canvSize.yMin;
        canvasInfo.yMax = $scope.canvSize.yMax;

        for (var i in $scope.graphs) {
            if ($scope.currentGraph === $scope.graphs[i]) {
                continue;
            }
            MrGraphie.render("canvas", canvasInfo, $scope.graphs[i]);
        }

        if ($scope.currentGraph) {
            MrGraphie.render("canvas", canvasInfo, $scope.currentGraph);
        }
    }

    $scope.reset = function () {
        $scope.graphs = [];
        $scope.currentGraph = null;
        $scope.graphTemplate = null;
        $scope.graphParams = null;
        $scope.canvSize = {
            xMin: -10,
            xMax: 10,
            yMin: -10,
            yMax: 10
        };
        delete canvasInfo.wasRendered;
        if (setId) {
            getSet(setId);
        } else {
            MrGraphie.render("canvas", canvasInfo, null);
            $scope.setCurrentGraph();
        }

    }

    $scope.saveImage = function () {
        MrGraphie.saveImage();
    }

    var getSet = function (id) {
        $http.get("/api/set/" + id).success(function (data) {
            for (var i in data.graphs) {
                var t = bundler.unbundle(data.graphs[i], GetGraph);
                $scope.graphs.push(t);
                $scope.canvSize.xMin = data.xMin;
                $scope.canvSize.xMax = data.xMax;
                $scope.canvSize.yMin = data.yMin;
                $scope.canvSize.yMax = data.yMax;
            }
            $scope.renderGraph();
        }).error(function (data) {
            console.log(data);
        });
    }

    $scope.saveSet = function () {
        var graphList = { graphs: [] };

        for (var i in $scope.graphs) {
            var bundle = bundler.bundle($scope.graphs[i]);
            graphList.graphs.push(bundle);
        }
        graphList.xMin = $scope.canvSize.xMin;
        graphList.xMax = $scope.canvSize.xMax;
        graphList.yMin = $scope.canvSize.yMin;
        graphList.yMax = $scope.canvSize.yMax;

        if (setId) {
            $http.put("/api/set/" + setId, graphList).success(function (data) {
                $location.path("/ngGraphList");
            }).error(function (data) {
                console.log(data);
            });
        } else {
            $http.post("/api/set/", graphList).success(function (data) {
                $location.path("/ngGraphList");
            }).error(function (data) {
                console.log(data);
            });

        }

    }

    $scope.reset();

});